/*
 *    Copyright 2006-2021 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.mybatis.generator.codegen;

import static org.mybatis.generator.internal.util.JavaBeansUtil.getGetterMethodName;

import java.util.List;
import java.util.Properties;

import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.config.PropertyRegistry;

public abstract class AbstractJavaGenerator extends AbstractGenerator {
	public abstract List<CompilationUnit> getCompilationUnits();

	private final String project;

	protected AbstractJavaGenerator(String project) {
		this.project = project;
	}

	public String getProject() {
		return project;
	}

	public static Method getGetter(Field field) {
		Method method = new Method(getGetterMethodName(field.getName(), field.getType()));
		method.setReturnType(field.getType());
		method.setVisibility(JavaVisibility.PUBLIC);
		String s = "return " + field.getName() + ';'; //$NON-NLS-1$

		method.addBodyLine(s);
		return method;
	}

	public String getRootClass() {
		String rootClass = introspectedTable.getTableConfigurationProperty(PropertyRegistry.ANY_ROOT_CLASS);
		if (rootClass == null) {
			Properties properties = context.getJavaModelGeneratorConfiguration().getProperties();
			rootClass = properties.getProperty(PropertyRegistry.ANY_ROOT_CLASS);
		}

		return rootClass;
	}

	protected void addDefaultConstructor(TopLevelClass topLevelClass) {
		topLevelClass.addMethod(getDefaultConstructor(topLevelClass));
	}

	protected void addDefaultConstructorWithGeneratedAnnotatoin(TopLevelClass topLevelClass) {
		topLevelClass.addMethod(getDefaultConstructorWithGeneratedAnnotation(topLevelClass));
	}

	private Method getDefaultConstructor(TopLevelClass topLevelClass) {
		Method method = getBasicConstructor(topLevelClass);
		addGeneratedJavaDoc(method);
		return method;
	}

	private Method getDefaultConstructorWithGeneratedAnnotation(TopLevelClass topLevelClass) {
		Method method = getBasicConstructor(topLevelClass);
		addGeneratedAnnotation(method, topLevelClass);
		return method;
	}

	private Method getBasicConstructor(TopLevelClass topLevelClass) {
		Method method = new Method(topLevelClass.getType().getShortName());
		method.setVisibility(JavaVisibility.PUBLIC);
		method.setConstructor(true);
		method.addBodyLine("super();"); //$NON-NLS-1$
		return method;
	}

	private void addGeneratedJavaDoc(Method method) {
		context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
	}

	private void addGeneratedAnnotation(Method method, TopLevelClass topLevelClass) {
		context.getCommentGenerator().addGeneralMethodAnnotation(method, introspectedTable,
				topLevelClass.getImportedTypes());
	}
}
